<?php
namespace app\common\model;
use app\common\library\mstching\PrintHelper;
use app\common\library\mstching\Feie;
use app\common\library\mstching\YiLian;

/**
 * 云打印机模型
 */
class Printer extends BaseModel
{
    protected $name = 'printer';
	
	/**
     * 品牌
     */
    public function getBrandAttr($value)
    {
        $status = [10 => '对对机', 20 => '飞鹅', 30 => '易联云'];
        return ['text' => $status[$value], 'value' => $value];
    }
	
	/**
	 * 关联门店
	 */
	public function shop()
	{
	    return $this->hasOne('Shop', 'shop_id', 'shop_id');
	}
	
	/**
     * 设备状态 - 自动完成
	 * 返回数据格式："}{"State":0,"Code":200,"Message":"成功"}
	 *State:状态值(-1错误 0正常 1缺纸 2温度保护报警 3 忙碌 4 离线)
     */
    public function getUuidAttr($value,$data)
    {
		$Message = '正常';
		if($data['brand']==10){
			$helper = new PrintHelper();
			$Message = $helper->getDeviceState($value);//查询设备状态	
		}
		if($data['brand']==20){
			$client = new Feie();
			$Message = $client->queryPrinterStatus($value);//查询设备状态	
		}
		if($data['brand']==30){
			$client = new YiLian();
			$Message = $client->queryPrinterStatus($value);//查询设备状态	
		}
		
        return ['text' => $Message, 'value' => $value];
    }

    /**
     * 获取打印机列表
     */
    public function getList($shop_id=0)
    {
		// 筛选条件
        $filter = [];
        $shop_id > 0 && $filter['shop_id'] = $shop_id;
        return $this->with(['shop'])->where($filter)->order('printer_id','desc')->select();
    }
	
    /**
     * 获取打印机详情
     */
    public static function detail($printer_id)
    {
        return self::get($printer_id,['shop']);
    }
	/**
	 * 打印小票
	 * $data 打印的数据
	 * $p_n 打印数量
	 */
	public function gotoPrint($data, $p_n)
	{
		//获取门店打印机列表
		if($list = $this->getList($data['shop_id'])){
			/*//计算在线设备数量
			$line_printer = array();//在线打印机列表
			for($n=0;$n<sizeof($list);$n++){
				array_push($line_printer,$list[$n]);
			}*/
			//如果有设备
			for($n=0;$n<sizeof($list);$n++){
				if($list[$n]['brand']['value']==10){
					//对对机
					$helper = new PrintHelper();
					$content = $helper->make_order_templet($data);//生成要打印的模板
					//打印份数
					for($m=0;$m<$p_n;$m++){
						//$task_id  打印成功返回任务编号 否则返回 false
						$task_id = $helper->printContent($list[$n]['uuid']['value'],$content,$list[$n]['open_user_id']);
						for($i=0;$i<5;$i++){
							$task_id = $helper->printContent($list[$n]['uuid']['value'],$content,$list[$n]['open_user_id']);
							if($task_id){
								break;
							}
						}
						if(!$task_id){
							$task_id = '任务提交失败';
						}
						//记录$task_id
						$model = new PrinterLog;
						$model->save([
							'task_id' => $task_id,
							'open_user_id' => $list[$n]['open_user_id'],
							'uuid' => $list[$n]['uuid']['value'],
							'content' => $content,
							'order_id' => $data['order_id'],
							'shop_id' => $data['shop_id'],
							'wxapp_id' => $data['wxapp_id']
						]);
					}
					break;
				}
				if($list[$n]['brand']['value']==20){
					//飞鹅
					$client = new Feie();
					$res = $client->printMsg($data,$list[$n]['open_user_id'],$p_n);//返回订单打印任务编号
					break;
				}
				if($list[$n]['brand']['value']==30){
					//易联云
					$client = new YiLian();
					$res = $client->printMsg($data,$list[$n]['open_user_id'],$p_n);//返回数据 {"id":"订单号","origin_id":""}
					break;
				}
			}
		}
		$this->error = '未添加打印设备';
        return false;
	}
}
